package org.example.school.controller;

import org.example.school.business.HomeWorkInfo;
import org.example.school.common.Result;
import org.example.school.entity.Assignments;
import org.example.school.model.dto.AssignmentsDTO;
import org.example.school.model.dto.TeacherAssignmentSubmissionDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 作业管理控制器
 * 处理教师发布作业、查看作业列表等操作
 */
@RestController
@RequestMapping("/homework")
@CrossOrigin
public class HomeWorkController {

    @Autowired
    private HomeWorkInfo homeWorkInfo;

    /**
     * 发布作业
     * @param assignmentsDTO 作业信息
     * @return 发布结果
     */
    @PostMapping("/assign")
    public Result<String> assignHomework(@RequestBody AssignmentsDTO assignmentsDTO) {
        return homeWorkInfo.assignHomework(assignmentsDTO);
    }

    /**
     * 根据班级ID查询作业列表
     * @param classId 班级ID
     * @return 作业列表
     */
    @GetMapping("/list/{classId}")
    public Result<List<Assignments>> getHomeworkListByClassId(@PathVariable Integer classId) {
        return homeWorkInfo.getHomeworkListByClassId(classId);
    }
    
    /**
     * 获取当前教师所有班级的作业列表
     * @return 作业列表
     */
    @GetMapping("/list")
    public Result<List<Assignments>> getAllHomeworkList() {
        return homeWorkInfo.getAllHomeworkList();
    }
    
    /**
     * 获取作业的提交情况
     * @param assignmentId 作业ID
     * @return 作业提交列表
     */
    @GetMapping("/submissions/{assignmentId}")
    public Result<List<TeacherAssignmentSubmissionDTO>> getAssignmentSubmissions(@PathVariable Long assignmentId) {
        return homeWorkInfo.getAssignmentSubmissions(assignmentId);
    }
    
    /**
     * 评分作业
     * @param submissionId 提交记录ID
     * @param grade 分数
     * @param feedback 评语
     * @return 评分结果
     */
    @PostMapping("/grade/{submissionId}")
    public Result<String> gradeAssignmentSubmission(
            @PathVariable Long submissionId,
            @RequestParam String grade,
            @RequestParam String feedback) {
        return homeWorkInfo.gradeAssignmentSubmission(submissionId, grade, feedback);
    }
    
    /**
     * 退回作业
     * @param submissionId 提交记录ID
     * @param feedback 退回原因
     * @return 退回结果
     */
    @PostMapping("/reject/{submissionId}")
    public Result<String> rejectAssignmentSubmission(
            @PathVariable Long submissionId,
            @RequestParam String feedback) {
        return homeWorkInfo.rejectAssignmentSubmission(submissionId, feedback);
    }
}